<?php

namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Models\Option;
use App\Models\Article;
use Carbon\Carbon,DB;
use Wechat;
class ApiController extends Controller
{

    public function getalldata(){
        $options = Option::where('status','!=',0)->get();
        $datas = [];
        foreach($options as $k=>$o){
            $datas['datas'][]=get_option_data($o->code);
        }
        //$data = get_option_data($symbol);
        echo json_encode($datas);
    }

    public function getdata($symbol){
        $data = get_option_data($symbol);
        echo json_encode($data);
    }

    /**
     * [getperioddata 分时数据]
     * @param  [type] $symbol [description]
     * @return [type]         [description]
     */
    public function getperioddata($symbol){
        $data = get_period_data($symbol);
        $option = Option::select('id','name', 'code','times')->where('code','=',$symbol)->first();
        $times = $option->times;
        $times = explode(',',$times);
        
        $data = collect($data);
        //$data = $data->keyBy('Date');
        list($start,$end) = explode('-',$times[0]);
        list($start1,$end1) = explode('-',$times[count($times)-1]);
        //$last = array_pop($times);
        //echo date('Y-m-d H:i:s',strtotime($start)),'<hr>',$end,'<hr>';
        // $data = $data->keyBy(function ($item) {
        //     return date('H:i',strtotime($item->Date));
        // });
        //if(date('H:i')>$start){
             $data = $data->filter(function ($item) use($start,$end1) {
                return date('H:i',strtotime($item->Date))>= $start || date('H:i',strtotime($item->Date))<= $end1;
            });
        //}
       

        $data = $data->values();
        //dd($data->all());
        echo json_encode($data->all());
    }

    public function getklinedata($symbol,$time,$return_t=3){
        $data = get_k_data($symbol,$time,$return_t);
     
        echo json_encode($data);
    }

    public function getorderdata($id){
        $data = Order::where('id','=',$id)->first();
        //echo $data;
        echo json_encode($data);
        //var_dump($data);
    }

}
